package com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice;

import cn.hutool.core.collection.CollUtil;
import com.wanjia.estate.pojo.entity.Details;
import com.wanjia.estate.pojo.httpservice.Jmhxx;
import com.wanjia.estate.service.httpservice.BdcHyZkHttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class EnquiryOfMinorsService {

    @Autowired
    BdcHyZkHttpService bdcHyZkHttpService;

    public List<Details> EnquiryOfMinors(Details details) {
        List<Details> list = new ArrayList<>();
        //创建居民户对象
        Jmhxx.Request request = new Jmhxx.Request();
        Jmhxx.RequestData data = new Jmhxx.RequestData();
        data.setSfzh(details.getIdcardno());
        request.setData(CollUtil.newArrayList(data));
        Jmhxx.Response jmhxx = bdcHyZkHttpService.jmhxx(request);

        List<Jmhxx.Result> results = jmhxx.getData().getHcyXx().getResults();

        //List<Jmhxx.Result> list1=new ArrayList<>();
        Boolean huzhu = false;

        //判断是否为户主
        for (Jmhxx.Result result : results) {


            String yhzgx = result.getYhzgx();
            if (yhzgx == "02" && Objects.equals(result.getGmsfhm(), details.getIdcardno())) {
                huzhu = true;
            }

            //得到现在年龄
            int years = result.calculateYear();

            if (years < 18) {
                log.info("有未成年人");
                Details details1 = new Details();
                details1.setName(result.getXm());
                details1.setIdcardno(result.getGmsfhm());
                list.add(details1);
            }
        }

        //判断是否存在未成年
        if (results.isEmpty()) {
            log.info("无未成年人");
        } else if (huzhu) {
            for (Jmhxx.Result result : results) {
                String dh = result.getYhzgx().substring(0, 1);
                //yhzgx的代号2开头和3开头的都是子女
                if (dh == "2" || dh == "3") {
                    Details details1 = new Details();
                    details1.setName(result.getXm());
                    details1.setIdcardno(result.getGmsfhm());
                    list.add(details1);
                }
            }
        } else {
            log.info("线下处理");
        }

        return list;
    }


    //返回未成年人信息
    public List<Details> getChildren(Details details) {
        List<Details> list = new ArrayList<>();
        //创建居民户对象
        Jmhxx.Request request = new Jmhxx.Request();
        Jmhxx.RequestData data = new Jmhxx.RequestData();
        data.setSfzh(details.getIdcardno());
        request.setData(CollUtil.newArrayList(data));
        Jmhxx.Response jmhxx = bdcHyZkHttpService.jmhxx(request);

        List<Jmhxx.Result> results = jmhxx.getData().getHcyXx().getResults();

        Boolean huzhu = false;
        for (Jmhxx.Result result : results) {
            String yhzgx = result.getYhzgx();
            if (yhzgx == "02" && Objects.equals(result.getGmsfhm(), details.getIdcardno())) {
                huzhu = true;
            }

            //得到现在年龄
            int years = result.calculateYear();

            if (years < 18) {
                log.info("有未成年人");
                Details details1 = new Details();
                details1.setName(result.getXm());
                details1.setIdcardno(result.getGmsfhm());
                list.add(details1);
            }
        }

        //判断是否存在未成年
        if (results.isEmpty()) {
            log.info("户口信息为空");
        } else if (huzhu) {
            for (Jmhxx.Result result : results) {
                String dh = result.getYhzgx().substring(0, 1);
                //yhzgx的代号2开头和3开头的都是子女
                if (dh == "2" || dh == "3") {
                    Details details1 = new Details();
                    details1.setName(result.getXm());
                    details1.setIdcardno(result.getGmsfhm());
                    list.add(details1);
                }
            }
        } else {
            log.info("其他情况");
        }

        return list;
    }



    //返回成年人信息
    public List<Details> getAdults(Details details) {
        List<Details> list = new ArrayList<>();
        //创建居民户对象
        Jmhxx.Request request = new Jmhxx.Request();
        Jmhxx.RequestData data = new Jmhxx.RequestData();
        data.setSfzh(details.getIdcardno());
        request.setData(CollUtil.newArrayList(data));
        Jmhxx.Response jmhxx = bdcHyZkHttpService.jmhxx(request);

        List<Jmhxx.Result> results = jmhxx.getData().getHcyXx().getResults();

        Boolean huzhu = false;
        for (Jmhxx.Result result : results) {
            String yhzgx = result.getYhzgx();
            if (yhzgx == "02" && Objects.equals(result.getGmsfhm(), details.getIdcardno())) {
                huzhu = true;
            }

            //得到现在年龄
            int years = result.calculateYear();

            if (years > 18) {
                log.info("有成年人");
                Details details1 = new Details();
                details1.setName(result.getXm());
                details1.setIdcardno(result.getGmsfhm());
                list.add(details1);
            }
        }

        //判断是否存在成年人
        if (results.isEmpty()) {
            log.info("户口信息为空");
        } else if (huzhu) {
            for (Jmhxx.Result result : results) {
                String dh = result.getYhzgx().substring(0, 2);
                //yhzgx的代号2开头和3开头的都是子女
                if (dh == "50" || dh == "51"||dh == "52") {
                    Details details1 = new Details();
                    details1.setName(result.getXm());
                    details1.setIdcardno(result.getGmsfhm());
                    list.add(details1);
                }
            }
        } else {
            log.info("其他情况");
        }

        return list;
    }
}
